package form;

/**
 * Purpose of this class is to be a shell for the
 * different types of forms we have. Re-use :)
 * 
 * @author Simon Zimmermann.
 */
public class FormField extends Form {

    private String name;
    private String labelName;
    private String fieldValue;
    private String customRegex;
    private FieldType fieldType;
    private boolean optional = false;

    /**
     * Unbound state of field.
     * @param name
     */
    protected FormField(String name, FieldType fieldType) {
        this.name = name.toLowerCase();
        this.labelName = name;
        this.fieldType = fieldType;
    }

    /**
     * Checks the validy of a value. Make sure we avoid
     * hard to catch exceptions. Therefore the extra null check.
     * A field can be optional - if it is empty and optional it will
     * pass the test.
     *
     * If a Field is not empty, it will be tested for regex, never the less.
     * @return true or false. 
     */
    public boolean isFieldValid() {
        if (isOptional() && fieldValue == null) {
            return true;
        } else {
            if (fieldType != null && fieldValue != null) {
                FieldRegex regex = new FieldRegex();
                if (customRegex != null) { 
                    return regex.testString(customRegex, fieldValue);
                } else {
                    return regex.isValid(fieldType, fieldValue);
                }
            }
        }
        return false;
    }

    /**
     * Returns HTML specific for a Form Field Tag. 
     * @return string with the correct HTML tag.
     */
    public String getFieldHtml() {
        return "";
    }

    // Getters, setters. 
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLabelName() {
        return labelName;
    }

    public void setLabelName(String labelName) {
        this.labelName = labelName;
    }

    public void setFieldValue(String fieldValue) {
        this.fieldValue = fieldValue;
    }

    public String getFieldValue() {
        return fieldValue;
    }

    public void setFieldType(FieldType fieldType) {
        this.fieldType = fieldType;
    }

    public FieldType getFieldType() {
        return fieldType;
    }

    public boolean isOptional() {
        return optional;
    }

    public void setOptional(boolean optional) {
        this.optional = optional;
    }

    public String getCustomRegex() {
        return customRegex;
    }

    public void setCustomRegex(String customRegex) {
        this.customRegex = customRegex;
    }
}